package com.syedu.cels.dao.impl;

import com.syedu.cels.dao.BaseDao;
import com.syedu.cels.entity.Vocabulary;
import com.syedu.cels.entity.Word;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * description：Dao的实现类
 */
public class BaseDaoImpl implements BaseDao {
    @Override
    public List<Word> selectWords() throws Exception {
        List<String> datas = Files.readAllLines(Paths.get("data/w.dic"));
        List<Word> words = new ArrayList<>();
        Word word = null;
        for (String str : datas) {
            word = new Word(str.split(" ")[0], Arrays.asList(str.split(" ")[1].split(":")));
            words.add(word);
        }
        return words;
    }

    @Override
    public List<Vocabulary> selectVocabularies() throws Exception {
        List<String> datas = Files.readAllLines(Paths.get("data/v.dic"));
        List<Vocabulary> vocabularies = new ArrayList<>();
        Vocabulary vocabulary = null;
        for (String str : datas) {
            String[] arr = str.split("#");
            vocabulary = new Vocabulary(arr[0], Arrays.asList(arr[1].split(":")));
            if (arr.length == 3) {
                vocabulary.setAbbreviation(arr[2]);
            }
            vocabularies.add(vocabulary);
        }
        return vocabularies;
    }

}